package java6703.lesson01.selection;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Random;

public class lesson01 extends Application {

    private Label label;
    private Label label2;
    private Object[] list;
    private Random random = new Random();
    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        //创建树状结构
        VBox root = new VBox(30);     //创建一个垂直布局的VBox的盒子
        Button button = new Button("随机选人");   //创建一个按键
        label = new Label("212106703  陈杰");
        label2 = new Label("恭喜学号和姓名被选中");
        //VBox中添加多个控件
        root.getChildren().addAll(this.label,button,label2);     //在根节点的孩子中放入按键


        root.setAlignment(Pos.CENTER);      //对齐方式

        //用根节点创建Scene
        Scene scene = new Scene(root, 400, 300);    //窗口大小设置

        //为窗口指定Scene
        primaryStage.setScene(scene);
        primaryStage.setTitle("随机选人");
        primaryStage.show();


        EventHandler<ActionEvent> lister = new MyEvenHandler();
        button.setOnAction(lister);

        try {    //从StudentList.txt中读取学生名单————数组
            list = Files.lines(Paths.get("01Lesson6703/data/StudentList.txt")).toArray();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    //实现监听接口，在事件处理方法中完成任务
    private class MyEvenHandler implements EventHandler<ActionEvent>{

        @Override
        public void handle(ActionEvent event) {

            int i = random.nextInt(96);

            label2.setText("恭喜"+(String)list[i]+"被选中");

        }
    }
}
